using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall;
using FlatRedBall.Input;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Graphics.Animation;
using FlatRedBall.Graphics.Particle;

using FlatRedBall.Math.Geometry;
using FlatRedBall.Math.Splines;
using BitmapFont = FlatRedBall.Graphics.BitmapFont;
using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;
using NereusGame.Entities.HBodies;

#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;


#endif

namespace NereusGame.Entities
{
	public partial class Gravitatable
	{
        protected float Mass;
        protected Vector3 AccelDueToGravity;
        protected Vector3 AccelWithoutGravity;

		private void CustomInitialize()
		{


		}

		private void CustomActivity()
		{


		}

		private void CustomDestroy()
		{

            
		}

        private static void CustomLoadStaticContent(string contentManagerName)
        {


        }

        public void Gravitate(List<Star> Stars, List<Planet> Planets, float g)
        {
            AccelDueToGravity = Vector3.Zero;
            foreach (Star s in Stars)
                AccelDueToGravity += calculateGravity(s, g);

            foreach (Planet p in Planets)
                AccelDueToGravity += calculateGravity(p, g);

            AccelWithoutGravity = Acceleration;
            Acceleration += AccelDueToGravity;
        }

        private Vector3 calculateGravity(HBody t, float g)
        {
            float force;

            Vector3 direction = t.Position - Position;
            float dist = direction.Length();

            if (dist > t.getGravRange())
                return Vector3.Zero;

            force = (t.getMass() * Mass) / (float)Math.Pow(dist, 2);
            return (g * direction * force);
        }

	}
}
